#ifdef OS_ENABLE_STIMERS


#if     OS_STIMER_SIZE == 1
    #define OS_STIMER_RUN   0x80
#elif   OS_STIMER_SIZE == 2
    #define OS_STIMER_RUN   0x8000
#elif   OS_STIMER_SIZE == 4
    #define OS_STIMER_RUN   0x80000000
#endif


/************************************************************************/
/* Check timer's states                                                 */
/************************************************************************/
#define OS_CheckSTimer(stimer_id)   (!(OS_STimers[stimer_id] & OS_STIMER_RUN))
#define OS_IsSTimerRun(stimer_id)   (OS_STimers[stimer_id] & OS_STIMER_RUN)


/************************************************************************/
/* Run timer for counting                                               */
/************************************************************************/

#define OS_RunSTimer(stimer_id, time)   \
    {                                   \
        OS_StopSTimer(stimer_id);       \
        OS_STimers[stimer_id] = -(time); \
    }
    // At the end of this macro we can't set bit Run since before we do it,
    // flag can became clear in interrupt.


/************************************************************************/
/* Stop/pause timer                                                     */
/************************************************************************/

#define OS_StopSTimer(stimer_id)    OS_STimers[stimer_id] &= ~OS_STIMER_RUN
#define OS_PauseSTimer(stimer_id)    OS_STimers[stimer_id] &= ~OS_STIMER_RUN


/************************************************************************/
/* Get remaining time of counting                                       */
/************************************************************************/

#define OS_GetSTimer(stimer_id)    (OS_CheckSTimer(stimer_id)? 0:(-OS_STimers[stimer_id]))

/************************************************************************/
/* Continue counting after pause                                        */
/************************************************************************/

#define OS_ContinueSTimer(stimer_id)    OS_STimers[stimer_id] |= OS_STIMER_RUN


/************************************************************************/
/* Wait for timer overflow                                              */
/************************************************************************/
#define OS_WaitSTimer(stimer_id)                    \
    {                                               \
        _OS_WAIT_EVENT(OS_CheckSTimer(stimer_id));  \
    }


/************************************************************************/
/* Delay current task using static timer                                */
/************************************************************************/
#define OS_DelaySTimer(stimer_id, delay)    \
    {                                       \
        OS_RunSTimer(stimer_id, delay);     \
        OS_WaitSTimer(stimer_id);           \
    }



#endif  // OS_ENABLE_STIMERS





